package com.bycx.core.service;

import com.bycx.core.mapper.BaseMapper;
import com.bycx.core.util.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yiqr on 2017/6/21.
 */
public abstract class BaseService<T> implements IBaseService<T>{
	public static final Log log = LogFactory.getLog(BaseService.class);
	private BaseMapper<T> baseMapper;
	public void setBaseMapper(BaseMapper<T> baseMapper) {
		this.baseMapper = baseMapper;
	}
	@Override
	public T getByPrimaryKey(Serializable primaryKey) {
		return baseMapper.getByPrimaryKey(primaryKey);
	}
	@Override
	public T get(Map<String, Object> params) {
		return baseMapper.get(params);
	}
	@Override
	public List<T> getList(Map<String, Object> params) {
		return baseMapper.getList(params);
	}
	@Override
	@Transactional
	public void save(T t) {
		baseMapper.insert(t);
	}
	@Override
	@Transactional
	public void updateByPrimaryKey(T t) {
		baseMapper.updateByPrimaryKey(t);
	}
	@Override
	@Transactional
	public void deleteByPrimaryKey(Serializable primaryKey) {
		if(primaryKey==null){
			return;
		}
		if("".equals(((String)primaryKey).trim())){
			return;
		}
		baseMapper.deleteByPrimaryKey(primaryKey);
	}
	@Override
	@Transactional
	public void delete(List<String> ids) {
		if(ids==null||ids.size()==0){
			return;
		}
		baseMapper.delete(ids);
	}
	@Override
	@Transactional
	public void deleteObj(List<T> list) {
		if(list==null||list.size()==0){
			return;
		}
		baseMapper.deleteObj(list);
	}
	
	@Override
	public T get(String propName, String value) {
		if(StringUtils.isEmpty(propName)){
			return null;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(propName, value);
		return this.get(map);
	}
	
	@Override
	public List<T> getList(String propName, String value) {
		if(StringUtils.isEmpty(propName)){
			return null;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(propName, value);
		return getList(map);
	}
}
